import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue'),
    meta:{
      title:'登录'
    }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/Register.vue'),
    meta:{
      title:'注册'
    }
  },
  {
    path: '/noteclass',
    name: 'noteclass',
    component: () => import('../views/NoteClass.vue'),
    meta:{
      title:'笔记分类'
    }
  },
  {
    path: '/notelist',
    name: 'notelist',
    component: () => import('../views/NoteList.vue'),
    meta:{   //tap页的标题
      title:'笔记列表'
    }
  },
  {
    path: '/noteDetail',
    name: 'noteDetail',
    component: () => import('../views/NoteDetail.vue'),
    meta:{   //tap页的标题
      title:'笔记详情'
    }
  },
  {
    path: '/notePublish',
    name: 'notePublish',
    component: () => import('../views/NotePublish.vue'),
    meta:{   //tap页的标题
      title:'笔记发布'
    }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

//路由守卫
const whiteList = ['/login', '/register']  //只有这两个页面可以跳
router.beforeEach((to, from, next) => {
  // console.log(to,from);
  document.title=to.meta.title  //浏览器标签的名字
  if (whiteList.includes(to.path)) {
    //from起 to终 放开next，没有next就一直停留在空中到不了to,继续往下
    next()
  }else{
    const userInfo=sessionStorage.getItem('userInfo')
    if (userInfo) {
      next()
    }else{
      router.push('/login')
    }
  }

})

export default router
